﻿using KZ.Express.DAL;
using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;

namespace KZ.Express.Task
{
    public class OrderTask : TaskBase
    {
        private new OrderENT _entity;
        private OrderDAL _orderDAL;
        private PriceListDAL _priceListDAL;
        private PriceListDetailDAL _priceListDetailDAL;

        public OrderTask()
            : base()
        {
            _orderDAL = new OrderDAL();
            _priceListDAL = new PriceListDAL();
            _priceListDetailDAL = new PriceListDetailDAL();
            base._entity = _entity = new OrderENT();
            _entitytype = _entity.GetType();
        }

        public DataTable LoadSupplierCarrier()
        {
            return _dalBase.SelectDataTable<PartnerCarrierENT>(new SearchCriteria());
        }

        public DataTable LoadOriginalPriceList(int carrierID, int supplierID, DateTime date)
        {
            DataTable dt = _priceListDAL.SelectOriginalPriceList(carrierID, supplierID, date);
            DataRow row = dt.NewRow();
            dt.Rows.InsertAt(row, 0);

            return dt;
        }

        public DataTable LoadCustomerPriceList(int carrierID, int supplierID, int customerID, DateTime date)
        {
            DataTable dt = _priceListDAL.SelectCustomerPriceList(carrierID, supplierID, customerID, date);
            DataRow row = dt.NewRow();
            dt.Rows.InsertAt(row, 0);

            return dt;
        }

        public decimal LoadPrice(int priceListID, int carrierID, int countryID, string type, decimal weight)
        {
            return _priceListDetailDAL.SelectPrice(priceListID, carrierID, countryID, type, weight);
        }

        public decimal LoadOriginalPrice(int supplierID, int carrierID, int countryID, string type, decimal weight, DateTime date, out int priceListID)
        {
            return _priceListDetailDAL.SelectOriginalPrice(supplierID, carrierID, countryID, type, weight, date, out priceListID);
        }

        public decimal LoadCustomerPrice(int customerID, int carrierID, int supplierID, int countryID, string type, decimal weight, DateTime date, out int priceListID)
        {
            return _priceListDetailDAL.SelectCustomerPrice(customerID, carrierID, supplierID, countryID, type, weight, date, out priceListID);
        }

        public void Save(Dictionary<string, List<DataRow>> changedRows)
        {
            base._dalBase.BeginTransaction();
            try
            {
                foreach (KeyValuePair<string, List<DataRow>> item in changedRows)
                {
                    foreach (DataRow row in item.Value)
                    {
                        string partnerGroupName = Utility.StringParse(row["PartnerGroupName"]);
                        if (!String.IsNullOrEmpty(partnerGroupName))
                        {
                            PartnerGroupENT partnerGroup = new PartnerGroupENT();
                            partnerGroup.PartnerGroupID = Utility.IntParse(row["PartnerGroupID"]);
                            partnerGroup.PartnerGroupName = partnerGroupName;

                            if (partnerGroup.PartnerGroupID == 0) base._dalBase.Insert(partnerGroup);
                            else base._dalBase.Update(partnerGroup);
                        }
                    }
                }

                base._dalBase.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_SAVE.ToString());
            }
        }

        /// <summary>
        /// Update surcharge order by carrier
        /// </summary>
        /// <returns></returns>
        public void UpdateSurcharge(int carrierID, DateTime from, DateTime to, decimal? orivat, decimal? vat,
            decimal? orifuel, decimal? fuel, decimal? rate)
        {
            try
            {
                _orderDAL.UpdateSurcharge(carrierID, from, to, orivat, vat, orifuel, fuel, rate);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }

        /// <summary>
        /// Update surcharge order by customer
        /// </summary>
        /// <returns></returns>
        public void UpdateSurcharge(int customerID, DateTime from, DateTime to, decimal? orivat, decimal? vat,
            decimal? orifuel, decimal? fuel, decimal? rate, decimal? cost, decimal? vndListOptional, string description)
        {
            try
            {
                _orderDAL.UpdateSurcharge(customerID, from, to, orivat, vat, orifuel, fuel, rate, cost, vndListOptional, description);
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}